package com.kakaxi.mybilibili.module.home.find.view;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.pager.BaseFragmentPagerAdapter;
import com.kakaxi.mybilibili.base.NavigationActivity;
import com.kakaxi.mybilibili.module.search.view.SearchActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 排行榜活动类
 * Created by kakaxi on 16/11/25.
 */

public class FindRankActivity extends NavigationActivity {

    @BindView(R.id.tablayout)
    TabLayout tabLayout;

    @BindView(R.id.viewpager)
    ViewPager viewPager;

    /** 排行榜类型 **/
    public static final String EXTRA_RANK_TYPE = "type";
    /** 跳转某个标签 **/
    public static final String EXTRA_JUMP_TAB = "jumpTab";

    /** 全区排行榜 **/
    public static final int TYPE_ALL = 1;
    /** 原创排行榜 **/
    public static final int TYPE_NEW = 2;

    /** 原创排行榜标题 **/
    String[] newTypeTitleArray = new String[]{
            "原创", "全站", "番剧"
    };
    /** 全区排行榜标题 **/
    String[] allTypeTitleArray = new String[]{
            "番剧", "动画", "音乐", "舞蹈", "游戏", "科技",
            "生活", "鬼畜", "时尚", "娱乐", "电影", "电视剧"
    };

    @Override
    protected int getCurrentMenuItemId() {
        return R.menu.menu_nav;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_common_tab_pager;
    }

    @Override
    protected void setupViews() {
        ButterKnife.bind(this);
        showBackBtn();
    }

    @Override
    protected void initData() {
        int type = getIntent().getIntExtra(EXTRA_RANK_TYPE, -1);
        int jumpTab = getIntent().getIntExtra(EXTRA_JUMP_TAB, 0);

        // 初始化列表
        List<FindRankFragment> fragmentList = new ArrayList<>();
        String[] titleArray;
        if (type == TYPE_NEW) {
            setTitle("原创排行榜");
            titleArray = newTypeTitleArray;
        } else if (type == TYPE_ALL) {
            setTitle("全区排行榜");
            titleArray = allTypeTitleArray;
        } else {
            toastHandler("缺少参数");
            finish();
            return;
        }

        for (int i = 0; i < titleArray.length; i++) {
            fragmentList.add(FindRankFragment.newInstance(type, i, titleArray[i]));
        }
        // 设置数据, 并关联标签布局
        viewPager.setAdapter(new BaseFragmentPagerAdapter<>(getSupportFragmentManager(),
                fragmentList));
        tabLayout.setupWithViewPager(viewPager);

        // 设置当前的位置
        if (jumpTab >= 0 && jumpTab < fragmentList.size()) {
            viewPager.setCurrentItem(jumpTab);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_common, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                Bundle bundle = new Bundle();
                bundle.putInt(SearchActivity.EXTRA_TYPE, SearchActivity.TYPE_VIDEO);
                goActivity(SearchActivity.class, bundle);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
